<?
//根据账号ID 获取供应商/分销商
require_once("/var/www/html/new/d/class/MemberAccount.class.php");

//晒掉散客
$mobile_account = ' and length(account)<11';


// 获取供应商
function getDistors($le,$format=1,$real=false){
	$ida=array();
	$ids='';
	// 管理员
	if($_SESSION['memberID']==1){
		$str="SELECT id FROM pft_member WHERE dtype=0 AND status=0".$GLOBALS['mobile_account'];
		$GLOBALS['le']->query($str);
		while($row=$GLOBALS['le']->fetch_assoc())
			$ida[]=$row['id'];
		// $ids=explode(',',$ida);
		// $memberID= empty($ids) ? $_SESSION['memberID']:$ids;
        $memberID = $_SESSION['memberID'];
		$memObject = new \pft\Member\MemberAccount($le, $memberID);
		$myDistors=$ida;
	}else{
        
		$memberID= ($_SESSION['dtype']==6) ? $_SESSION['sid']:$_SESSION['memberID'];
		$memObject = new \pft\Member\MemberAccount($le, $memberID);
		$sql="SELECT parent_id FROM pft_member_relationship WHERE  son_id=$memberID AND ship_type=0 AND status=0";
		$GLOBALS['le']->query($sql);
		$myDistors=array();
		while($row=$GLOBALS['le']->fetch_assoc()){
			$myDistors[]=$row['parent_id'];
		}
	}
	//加上自己的
	$myDistors[]=$memberID;
	if(!$format)return $myDistors;
	if(count($myDistors)>0)  $DistorsNames = $memObject->getMemName($myDistors,$real);
	return $DistorsNames;
}// 获取分销商
function getRetail($le,$memberID=0,$format=1){
	if($memberID==0){
        $memberID= ($_SESSION['dtype']==6) ? $_SESSION['sid']:$_SESSION['memberID'];
    }
	$memObject = new \pft\Member\MemberAccount($le,$memberID);
    $no_cut = array('961'=>1);//这个数组里的帐号 不显示已经解除关系的分销商
    // 获取曾经建立过的，防止销售后再解除
    $sql="SELECT r.son_id FROM pft_member_relationship r LEFT JOIN pft_member m ON m.id=r.son_id WHERE r.son_id!={$memberID} and r.parent_id=$memberID AND r.ship_type=0 AND m.status=0".$GLOBALS['mobile_account'];
    if($no_cut[$_SESSION['sid']])
        $sql .= ' and r.status=0';
	$GLOBALS['le']->query($sql);
	$myDistors=array();
	while($row=$GLOBALS['le']->fetch_assoc()){
		$myDistors[]=$row['son_id'];
	}
	//加上自己的（自供应）
    if($_SERVER['SCRIPT_URL']!='/orderReport.html')//出售中的产品报表不应该出现自己
	    $myDistors[]=$memberID;
	// 不格式化 ，格式化会有第一个字的大写字母 例如 X-先行旅游
	if(!$format)	return $myDistors;
	if(count($myDistors)>0) {
        $DistorsNames = $memObject->getMemName($myDistors);
    }
	return $DistorsNames;
}
// 获取分销商
function _getRetail($le,$memberID=0,$format=1){
	print_r($_SESSION);
	if($memberID==0) $memberID=$_SESSION['memberID'];
	$memObject = new \pft\Member\MemberAccount($le,$memberID);
	// 获取曾经建立过的，防止销售后再解除
    $sql="SELECT r.son_id FROM pft_member_relationship r LEFT JOIN pft_member m ON m.id=r.son_id WHERE r.parent_id=$memberID AND r.ship_type=0 AND m.status=0";
	$GLOBALS['le']->query($sql);
	$myDistors=array();
	while($row=$GLOBALS['le']->fetch_assoc()){
		$myDistors[]=$row['son_id'];
	}
	//加上自己的（自供应）
	$myDistors[]=$memberID;
	// 不格式化 ，格式化会有第一个字的大写字母 例如 X-先行旅游
	if(!$format)
		return $myDistors;
	if(count($myDistors)>0) {
        $DistorsNames = $memObject->getMemName($myDistors);
    }
	return $DistorsNames;
	
	
}

// 集团账号获取全部（供应商和分销商）
function getAll($le,$memberSID,$format=1){
	$sql="select dname,cname,account,mobile,m.id,ship_type,son_id_type from pft_member_relationship r left join pft_member m on m.id=son_id where m.status in (0,3) and r.status=0 and parent_id=$memberSID and son_id_type in (0,3) and ship_type=1".$GLOBALS['mobile_account'];
	$GLOBALS['le']->query($sql);
	$myDistors=array();
	while($row=$GLOBALS['le']->fetch_assoc()){
		$myDistors[]=$row['id'];
	}
	if(!$format)
		return $myDistors;
	$memObject = new \pft\Member\MemberAccount($le,$memberSID);
	if(count($myDistors)>0) {
        $DistorsNames = $memObject->getMemName($myDistors);
    }
	return $DistorsNames;
}
// 平台管理员
function getAllMember($le,$memberSID=1, $dtype=null){
    $where = !is_null($dtype) ? "dtype={$dtype}" : "dtype<2";
	$sql="select id from pft_member where status=0 and $where".$GLOBALS['mobile_account'];
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$myDistors[]=$row['id'];
	}
	$memObject = new \pft\Member\MemberAccount($le,$memberSID);
	if(count($myDistors)>0) {
        $DistorsNames = $memObject->getMemName($myDistors);
    }
	return $DistorsNames;
}

function getAlliRetail($le,$memberID=0,$format=1){
	if($memberID==0){
        $memberID= ($_SESSION['dtype']==6) ? $_SESSION['sid']:$_SESSION['memberID'];
    }
	$memObject = new \pft\Member\MemberAccount($le,$memberID);
	if(!empty($_SESSION['alliance'])){
    $sql="select memberID from pft_union_member_info where fid='$memberID'";
	$GLOBALS['le']->query($sql);
	$myDistors=array();
	while($row=$GLOBALS['le']->fetch_assoc()){
		$myDistors[]=$row['memberID'];
	}
	//加上自己的（自供应）
	$myDistors[]=$memberID;
    //print_r($myDistors);
	}else{
		$sql = "select * from pft_distributor_agency where inviterID='$memberID'";
		$GLOBALS['le']->query($sql);
		$row=$GLOBALS['le']->fetch_assoc();
		//print_r($list);
	    $fid=$row['fid'];
        $sql2="select memberID from pft_union_member_info where fid='$fid'";
        $GLOBALS['le']->query($sql2);
        $myDistors=array();
        while($row=$GLOBALS['le']->fetch_assoc()){
         $myDistors[]=$row['memberID'];
         $myDistors[]=$fid;
        }
        //加上自己的（自供应）
        $myDistors[]=$memberID;
	}
	// 不格式化 ，格式化会有第一个字的大写字母 例如 X-先行旅游
	if(!$format)	return $myDistors;
	if(count($myDistors)>0) {
        $DistorsNames = $memObject->getMemName($myDistors);
    }
	//return $myDistors;
	return $DistorsNames;
}


?>